package top.frsf.pattern.c_behavioral.a_strategy.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.frsf.pattern.c_behavioral.a_strategy.send.IMsgSendStrategy;
import top.frsf.pattern.c_behavioral.a_strategy.send.MsgSendStrategyContext;
import top.frsf.pattern.c_behavioral.a_strategy.source.context.DatasourceContext;
import top.frsf.pattern.c_behavioral.a_strategy.source.entity.Datasource;
import top.frsf.pattern.c_behavioral.a_strategy.source.service.AbstractQuery;

import javax.annotation.Resource;

/**
 * @Author He Kuntao
 * @Date 2024/7/1 16:15
 * @Desc TODO
 **/
@RestController
@RequestMapping("/strategy")
public class StrategyTestController {

    @Resource
    private MsgSendStrategyContext msgSendStrategyContext;

    @GetMapping("/send/{name}")
    public String send(@PathVariable String name) {
        IMsgSendStrategy strategy = msgSendStrategyContext.getStrategyByName(name);
        return strategy == null ? "null" : strategy.sendMsg();
    }

    @GetMapping("/source/{name}")
    public String source(@PathVariable String name) {
        Datasource datasource = Datasource.builder().type(name).build();
        AbstractQuery query = DatasourceContext.getQuery(datasource.getType());
        return query.tableSql();
    }
}
